package com.swf.common.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.swf.common.base.FwsCharset;
import com.swf.common.exception.FwsException;
import com.swf.common.utils.FwsFileUtils;


public class FwsStringIOUtils{
	
	/**
	 * 读取文件成字符串
	 * @param file 需要读取的文件
	 * @param charst 读取文件的编码
	 * @return
	 * @throws IOException
	 */
	public static String read(File file,String charst) throws IOException {
		InputStream is = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(is,charst);
		BufferedReader br = new BufferedReader(isr);
		char[] cbuf = new char[1024];
		int len;
		StringBuffer sb = new StringBuffer();
		while ((len=br.read(cbuf))!=-1){
			sb.append(cbuf,0,len);
		}
		br.close();
		return sb.toString();
	}	
	
	/**
	 * 读取文件成字符串
	 * @param is 需要读取的文件流
	 * @param charst 读取文件的编码
	 * @return
	 * @throws IOException
	 */
	public static String read(InputStream is,String charst) throws IOException {
		InputStreamReader isr = new InputStreamReader(is,charst);
		BufferedReader br = new BufferedReader(isr);
		char[] cbuf = new char[1024];
		int len;
		StringBuffer sb = new StringBuffer();
		while ((len=br.read(cbuf))!=-1){
			sb.append(cbuf,0,len);
		}
		return sb.toString();
	}
	/**
	 * 使用系统默认编码读取文件
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String read(File file) throws IOException {
		return read(file,FwsCharset.defaultCharset);
	}
	
	/**
	 * 读取文件成字符串
	 * @param file 需要读取的文件
	 * @param charst 读取文件的编码
	 * @return
	 * @throws IOException
	 */
	public static String read(String fileName,String charst) throws IOException {
		InputStream is = new FileInputStream(FwsFileUtils.getFile(fileName));
		InputStreamReader isr = new InputStreamReader(is,charst);
		BufferedReader br = new BufferedReader(isr);
		char[] cbuf = new char[1024];
		int len;
		StringBuffer sb = new StringBuffer();
		while ((len=br.read(cbuf))!=-1){
			sb.append(cbuf,0,len);
		}
		return sb.toString();
	}	
	
	/**
	 * 使用系统默认编码读取文件成字符串
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static String read(String fileName) throws IOException{
		return read(fileName, FwsCharset.defaultCharset);
	}
	
	/**
	 * 将字符串输出到指定文件中,默认不追加
	 * @param file 文件
	 * @param str 字符串
	 * @param charst 字符编码
	 * @throws IOException
	 * @throws FwsException 
	 */
	public static void write(File file,String str,String charst) throws IOException, FwsException{
		write(file, str, charst, false);
	}
	
	/**
	 * 将字符串输出到指定文件中
	 * @param file 文件
	 * @param str 字符串
	 * @param charst 字符编码
	 * @param append 是否追加
	 * @throws IOException
	 * @throws FwsException 
	 */
	public static void write(File file,String str,String charst,boolean append) throws IOException, FwsException{
		if (!file.exists()) {
			if (!file.createNewFile()){
				throw new FwsException("create file failed!");
			}
		}
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,append), charst));
		bw.write(str);
		bw.flush();
		bw.close();
	}
	
	/**
	 * 使用默认字符集
	 * @param file
	 * @param str
	 * @throws IOException
	 * @throws FwsException
	 */
	public static void write(File file,String str) throws IOException, FwsException{
		write(file, str, FwsCharset.defaultCharset);
	}
	/**
	 * 将字符串输出到指定路径的文件中
	 * @param filePath
	 * @param str
	 * @param charst
	 * @throws IOException
	 * @throws FwsException
	 */
	public static void write(String filePath,String str,String charst) throws IOException, FwsException{
		File file = new File(filePath);
		write(file, str, charst);
	}
	/**
	 * 使用默认字符集
	 * @param filePath
	 * @param str
	 * @throws IOException
	 * @throws FwsException
	 */
	public static void write(String filePath,String str) throws IOException, FwsException{
		write(filePath, str, FwsCharset.defaultCharset);
	}
	
	
}
